package com.cyj.dream.core.util.page;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cyj.dream.core.constant.pagemodel.PageWrapper;
import com.cyj.dream.core.constant.pagemodel.Response;
import com.cyj.dream.core.util.bean.BeanCopierUtil;

import java.util.List;

/**
 * @Description: 分页转换器
 * @BelongsProject: DreamChardonnay
 * @BelongsPackage: com.cyj.dream.core.util
 * @Author: ChenYongJia
 * @CreateTime: 2021-09-26 10:33
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
public class DbPageConvertUtil {

    /**
     * Mybatis-plus 分页对象转换为返回页面的分页对象
     *
     * @param dbPage db层分页对象
     * @param voClz  页面层对象类类型
     * @return 页面分页对象
     * @author ChenYongJia
     */
    public static <V, P> Response<PageWrapper<V>> convert(IPage<P> dbPage, Class<V> voClz) {
        List<V> list = BeanCopierUtil.copyPropertiesForList(dbPage.getRecords(), voClz);
        return convert(dbPage, list);
    }

    /**
     * Mybatis-plus 分页对象转换为返回页面的分页对象
     *
     * @param dbPage db层分页对象
     * @param voList 外层已经处理过的页面层分页列表
     * @return 页面分页对象
     * @author ChenYongJia
     */
    public static <V, P> Response<PageWrapper<V>> convert(IPage<P> dbPage, List<V> voList) {
        Response<PageWrapper<V>> returnResponse = new Response<>();
        PageWrapper<V> pageWrapper = convertToPageWrapper(dbPage, voList);
        returnResponse.setCode(200);
        returnResponse.setData(pageWrapper);
        return returnResponse;
    }

    public static <V, P> PageWrapper<V> convertToPageWrapper(IPage<P> dbPage, List<V> voList) {
        PageWrapper<V> pageWrapper = new PageWrapper<>();
        pageWrapper.setPageNum(dbPage.getCurrent());
        pageWrapper.setPageSize(dbPage.getSize());
        pageWrapper.setTotal(dbPage.getTotal());
        pageWrapper.setTotalPage(dbPage.getPages());
        pageWrapper.setData(voList);
        return pageWrapper;
    }

}
